home *** CD-ROM | disk | FTP | other *** search
- import java.awt.image.ColorModel;
- import java.awt.image.IndexColorModel;
- import java.awt.image.RGBImageFilter;
-
- class ButtonFilter extends RGBImageFilter {
- boolean pressed;
- int defpercent;
- int border;
- int width;
- int height;
- ColorModel[] models = new ColorModel[7];
- ColorModel origbuttonmodel;
-
- public ButtonFilter(boolean press, int p, int b, int w, int h) {
- this.pressed = press;
- this.defpercent = p;
- this.border = b;
- this.width = w;
- this.height = h;
- }
-
- public void setHints(int hints) {
- super.setHints(hints & -5);
- }
-
- public void setColorModel(ColorModel model) {
- if (model instanceof IndexColorModel) {
- IndexColorModel icm = (IndexColorModel)model;
- this.models[0] = this.filterIndexColorModel(icm, false, false, 0);
- this.models[1] = this.filterIndexColorModel(icm, true, !this.pressed, this.defpercent);
- this.models[2] = null;
- if (this.pressed) {
- this.models[3] = this.filterIndexColorModel(icm, true, false, this.defpercent / 2);
- } else {
- this.models[3] = this.models[0];
- }
-
- this.models[4] = null;
- this.models[5] = this.filterIndexColorModel(icm, true, this.pressed, this.defpercent);
- this.models[6] = this.models[0];
- this.origbuttonmodel = model;
- super.consumer.setColorModel(this.models[3]);
- } else {
- super.setColorModel(model);
- }
- }
-
- public IndexColorModel filterIndexColorModel(IndexColorModel icm, boolean opaque, boolean brighter, int percent) {
- byte[] r = new byte[256];
- byte[] g = new byte[256];
- byte[] b = new byte[256];
- byte[] a = new byte[256];
- int mapsize = icm.getMapSize();
- icm.getReds(r);
- icm.getGreens(g);
- icm.getBlues(b);
- if (opaque) {
- icm.getAlphas(a);
-
- for(int i = 0; i < mapsize; ++i) {
- int rgb = this.filterRGB(icm.getRGB(i), brighter, percent);
- a[i] = (byte)(rgb >> 24);
- r[i] = (byte)(rgb >> 16);
- g[i] = (byte)(rgb >> 8);
- b[i] = (byte)rgb;
- }
- }
-
- return new IndexColorModel(((ColorModel)icm).getPixelSize(), mapsize, r, g, b, a);
- }
-
- public void buttonRanges(int y, int[] ranges) {
- ranges[0] = ranges[1] = 0;
- if (y < this.border) {
- ranges[2] = ranges[3] = ranges[4] = ranges[5] = this.width - y;
- } else if (y > this.height - this.border) {
- ranges[2] = ranges[3] = ranges[4] = ranges[5] = this.height - y;
- } else {
- ranges[2] = ranges[3] = this.border;
- ranges[4] = ranges[5] = this.width - this.border;
- }
-
- ranges[6] = ranges[7] = this.width;
- }
-
- public void setPixels(int x, int y, int w, int h, ColorModel model, byte[] pixels, int off, int scansize) {
- if (model != this.origbuttonmodel) {
- super.setPixels(x, y, w, h, model, pixels, off, scansize);
- } else {
- int[] ranges = new int[8];
- int x2 = x + w;
-
- for(int cy = y; cy < y + h; ++cy) {
- this.buttonRanges(cy, ranges);
-
- for(int i = 0; i < 7; ++i) {
- if (x2 > ranges[i] && x < ranges[i + 1]) {
- int cx1 = Math.max(x, ranges[i]);
- int cx2 = Math.min(x2, ranges[i + 1]);
- if (this.models[i] == null) {
- super.setPixels(cx1, cy, cx2 - cx1, 1, model, pixels, off + (cx1 - x), scansize);
- } else if (cx1 < cx2) {
- super.consumer.setPixels(cx1, cy, cx2 - cx1, 1, this.models[i], pixels, off + (cx1 - x), scansize);
- }
- }
- }
-
- off += scansize;
- }
-
- }
- }
-
- public int filterRGB(int x, int y, int rgb) {
- boolean brighter;
- int percent;
- if ((x >= this.border || y >= this.height - x) && (y >= this.border || x >= this.width - y)) {
- if (x < this.width - this.border && y < this.height - this.border) {
- if (!this.pressed) {
- return rgb & 16777215;
- }
-
- brighter = false;
- percent = this.defpercent / 2;
- } else {
- brighter = this.pressed;
- percent = this.defpercent;
- }
- } else {
- brighter = !this.pressed;
- percent = this.defpercent;
- }
-
- return this.filterRGB(rgb, brighter, percent);
- }
-
- public int filterRGB(int rgb, boolean brighter, int percent) {
- int r = rgb >> 16 & 255;
- int g = rgb >> 8 & 255;
- int b = rgb & 255;
- if (brighter) {
- r = 255 - (255 - r) * (100 - percent) / 100;
- g = 255 - (255 - g) * (100 - percent) / 100;
- b = 255 - (255 - b) * (100 - percent) / 100;
- } else {
- r = r * (100 - percent) / 100;
- g = g * (100 - percent) / 100;
- b = b * (100 - percent) / 100;
- }
-
- if (r < 0) {
- r = 0;
- }
-
- if (g < 0) {
- g = 0;
- }
-
- if (b < 0) {
- b = 0;
- }
-
- if (r > 255) {
- r = 255;
- }
-
- if (g > 255) {
- g = 255;
- }
-
- if (b > 255) {
- b = 255;
- }
-
- return rgb & -16777216 | r << 16 | g << 8 | b;
- }
- }
-